package com.kaishengit.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.kaishengit.pojo.Document;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.User;
import com.kaishengit.service.DocumentService;

@Controller
@Results({
		@Result(name = "saveSUCCESS", location = "document.action", type = "redirectAction"),
		@Result(name = "edit", location = "document.action", type = "redirectAction"),
		@Result(name = "del", location = "document.action", type = "redirectAction"),

})
public class DocumentAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private DocumentService documentService;
	private Document document;
	private String id;

	private User user;
	private Project project;
	private List<Document> documentList;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Action("document")
	public String document() {
		project = (Project) getFromSession("project");
		documentList = documentService.findAllByPro(project);

		return SUCCESS;
	}

	@Action("adddocument")
	public String add() {

		return SUCCESS;
	}

	@Action("toeditdocument")
	public String toeditDocument() {
		document = documentService.finById(id);

		return SUCCESS;
	}

	@Action("editdocument")
	public String editDoc() {
		System.out.println(id);
		project = (Project) getFromSession("project");
		user = (User) getFromSession("user");

		documentService.update(document, id);

		return "edit";
	}

	@Action("savedocument")
	public String save() {
		project = (Project) getFromSession("project");
		user = (User) getFromSession("user");

		documentService.save(document, project, user);
		return "saveSUCCESS";
	}

	@Action("deldocument")
	public String del() {
		//project = projectService.finById(id);
		documentService.del(id);
		return "del";
	}
	@Action("todocument")
	public String todocument(){
		document=documentService.finById(id);
		return SUCCESS;
	}
	
	@Autowired
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Document> getDocumentList() {
		return documentList;
	}

	public void setDocumentList(List<Document> documentList) {
		this.documentList = documentList;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

}
